﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using V5_Common;
using System.Data;
using V5_BLL;
using V5_Model;
using Newtonsoft.Json;

namespace V5_EShop.Modules.Product {
    /// <summary>
    /// ProductShoppingCart 的摘要说明
    /// </summary>
    public class ProductShoppingCart : IHttpHandler {
        int shopping_pro_count = 0, shopping_price_count = 0, shopping_pro_score = 0;
        HttpContext context;
        public void ProcessRequest(HttpContext _context) {
            context = _context;
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            string Action = InputHelper.SetString(context.Request["Action"]);
            int proid = InputHelper.SetNumber(context.Request.QueryString["pid"]);
            switch (Action.ToLower()) {
                case "del":
                    Bind_delshoppingitem(proid);
                    break;
                case "editshopping":
                    Bind_EditShopping(proid);
                    break;
                default:
                    Bind_addshoppingcart(proid);
                    break;
            }
        }

        public bool IsReusable {
            get {
                return false;
            }
        }

        private void Bind_RemoveShopping() {
            context.Session.Remove("dtProducts");
        }

        private void Bind_EditShopping(int proid) {
            string txtQuantity = InputHelper.SetString(context.Request["txtQuantity"]);
            shopping shop = new shopping();
            DataTable dtProducts = null;
            int m1 = 0, m2 = 0, m3 = 0, m4 = 0, m6 = 0;
            string jsonstr = "";
            if (context.Session["dtProducts"] == null) { }
            else {
                dtProducts = (DataTable)context.Session["dtProducts"];
                if (dtProducts != null && dtProducts.Rows.Count > 0) {
                    m6 = 0;
                    DataRow[] DataRows = dtProducts.Select(" pid=" + proid);
                    ModelProduct model = new BLLProduct().GetModel(proid, true);
                    //判断 是否超过库存
                    if (InputHelper.SetNumber(txtQuantity) > model.ProductCount) {
                        return;
                    }
                    //计算
                    foreach (DataRow item1 in DataRows) {
                        // item1["pid"] = proid;
                        item1["prototalprice"] = (InputHelper.SetNumber(model.ProductMemberPrice) * InputHelper.SetNumber(txtQuantity));//小计
                        // item1["promemberprice"] = model.ProductMemberPrice;
                        //item1["promarketprice"] = model.ProductMarketPrice;
                        item1["proquantity"] = txtQuantity;
                        // item1["profieldvalues"] = txtfieldvalues;
                        //item1["proname"] = model.ProductName;
                        //item1["propic"] = model.ProductBigPic;
                        //item1["quantity"] = dtProducts.Rows.Count + 1;
                    }
                    dtProducts.AcceptChanges();
                    //    //开始计算
                    //小计价格 m2
                    m2 = (InputHelper.SetNumber(model.ProductMemberPrice) * InputHelper.SetNumber(txtQuantity));
                    m1 = m2;//赠送积分
                    m3 = dtProducts.Rows.Count;//商品数量
                    //产品总金额
                    for (int i = 0; i < dtProducts.Rows.Count; i++) {
                        m6 += (InputHelper.SetNumber(dtProducts.Rows[i]["prototalprice"]));
                    }
                    m4 = m6;//积分总计
                    //    //计算结束
                    context.Session["dtProducts"] = dtProducts;//修改完毕后更新session

                    shop.i1 = m2;
                    shop.i2 = m2;
                    shop.i3 = m3;
                    shop.i4 = m6;
                    shop.i5 = 5;
                    shop.i6 = m6;
                    jsonstr = JsonConvert.SerializeObject(shop);
                    context.Response.Write(jsonstr);
                    context.Response.End();
                }
                context.Response.End();
            }
        }
        class shopping {
            public int i1;//赠送积分
            public int i2;//小计价格
            public int i3;//商品数量总计
            public int i4;//赠送积分总计
            public int i5;//花费积分总计
            public int i6;//产品金额总计
        }
        //添加购物
        private void Bind_addshoppingcart(int proid) {
            string txtfieldvalues = InputHelper.SetString(context.Request["txtfieldvalues"]);
            string txtQuantity = InputHelper.SetString(context.Request["txtQuantity"]);
            if (proid > 0) {
                ModelProduct model = new BLLProduct().GetModel(proid, true);
                if (model != null) {
                    DataTable dtProducts = null;
                    if (context.Session["dtProducts"] == null) {
                        dtProducts = new DataTable();
                        dtProducts.Columns.Add(new DataColumn("pid"));//产品ID
                        dtProducts.Columns.Add(new DataColumn("prototalprice"));//总价格
                        dtProducts.Columns.Add(new DataColumn("promemberprice"));//会员价
                        dtProducts.Columns.Add(new DataColumn("promarketprice"));//市场价 
                        dtProducts.Columns.Add(new DataColumn("proquantity"));//单个产品数量
                        dtProducts.Columns.Add(new DataColumn("profieldvalues"));//产品规格
                        dtProducts.Columns.Add(new DataColumn("proname"));//产品名称
                        dtProducts.Columns.Add(new DataColumn("propic"));//产品图片
                        dtProducts.Columns.Add(new DataColumn("proscore"));//增送积分
                    }
                    else {
                        dtProducts = (DataTable)context.Session["dtProducts"];
                    }
                    //添加产品
                    bool found = false;
                    //循环购物车 是否有重复的产品
                    for (int i = 0; i < dtProducts.Rows.Count; i++) {
                        if (Convert.ToInt32(dtProducts.Rows[i]["pid"]) == proid) {
                            found = true;
                            break;
                        }
                    }
                    //如果购物车没有产品 开始添加
                    if (!found) {
                        DataRow aProduct = dtProducts.NewRow();

                        aProduct["pid"] = proid;
                        aProduct["prototalprice"] = (InputHelper.SetNumber(model.ProductMemberPrice) * InputHelper.SetNumber(txtQuantity));//小计
                        aProduct["promemberprice"] = model.ProductMemberPrice;
                        aProduct["promarketprice"] = model.ProductMarketPrice;
                        aProduct["proquantity"] = txtQuantity;
                        aProduct["profieldvalues"] = txtfieldvalues;
                        aProduct["proname"] = model.ProductName;
                        aProduct["propic"] = model.ProductBigPic;
                        aProduct["proscore"] = InputHelper.SetNumber(model.ProductMemberPrice);//赠送积分
                        dtProducts.Rows.Add(aProduct);
                    }
                    context.Session["dtProducts"] = dtProducts;//添加完毕后更新session
                    //商品数量
                    shopping_pro_count = dtProducts.Rows.Count;
                    //商品总金额
                    for (int i = 0; i < dtProducts.Rows.Count; i++) {
                        shopping_price_count += InputHelper.SetNumber(dtProducts.Rows[i]["prototalprice"]);
                    }
                    //积分
                    shopping_pro_score = shopping_price_count;
                    //Repeater rp = (Repeater)this.FindControl("rpShoppingcartList");
                    //if (rp != null) {
                    //    rp.DataSource = dtProducts;
                    //    rp.DataBind();
                    //}
                }
            }
            else {
                DataTable dtProducts = null;
                if (context.Session["dtProducts"] != null) {
                    dtProducts = (DataTable)context.Session["dtProducts"];
                    //Repeater rp = (Repeater)this.FindControl("rpShoppingcartList");
                    //if (rp != null) {
                    //    rp.DataSource = dtProducts;
                    //    rp.DataBind();
                    //}
                }
            }
        }

        private void Bind_delshoppingitem(int proid) {
            if (proid > 0) {
                ModelProduct model = new BLLProduct().GetModel(proid, true);
                if (model != null) {
                    DataTable dtProducts = null;
                    if (context.Session["dtProducts"] != null) {
                        dtProducts = (DataTable)context.Session["dtProducts"];
                    }
                    else {
                        context.Response.Redirect(HttpsHelper.ComeUrl);
                    }

                    for (int i = 0; i < dtProducts.Rows.Count; i++) {
                        if (Convert.ToInt32(dtProducts.Rows[i]["pid"]) == proid) {
                            dtProducts.Rows[i].Delete();
                        }
                    }
                    dtProducts.AcceptChanges();
                    context.Session["dtProducts"] = dtProducts;//添加完毕后更新session
                    //
                    //商品数量
                    shopping_pro_count = dtProducts.Rows.Count;
                    //商品总金额
                    for (int i = 0; i < dtProducts.Rows.Count; i++) {
                        shopping_price_count += InputHelper.SetNumber(dtProducts.Rows[i]["prototalprice"]);
                    }
                    //积分
                    shopping_pro_score = shopping_price_count;

                    //Repeater rp = (Repeater)this.FindControl("rpShoppingcartList");
                    //if (rp != null) {
                    //    rp.DataSource = dtProducts;
                    //    rp.DataBind();
                    //}
                    //
                }
            }
        }
    }
}